package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.ValidateEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import butterknife.BindView;

/**
 * Created by wangguodong on 2017/8/26.
 */

public class SetCircleComplaintActivity extends BaseSwipeBackActivity {

    @BindView(R.id.switch_nolookme)
    Switch switch_nolookme ;
    @BindView(R.id.switch_nolookher)
    Switch switch_nolookher;

    PGService mPgService;

    private String friendId = "" ;
    private boolean open_nolookme = false ;
    private boolean open_nolookher = false ;
    private ImFriendEntivity imFriendEntivityK;

    public static void start(Activity activity, String friendId){
        Intent intent = new Intent(activity, SetCircleComplaintActivity.class);
        intent.putExtra("friendId", friendId);
        activity.startActivity(intent);

    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.set_circle_pawr);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        mPgService = PGService.getInstance();
        friendId = getIntent().getStringExtra("friendId");
        if (null==friendId || "".equals(friendId))friendId = "0";
        imFriendEntivityK = ToolsUtils.getMyFriendForId(Long.parseLong(friendId));
        if (null!=imFriendEntivityK){
            if ("0".equals(imFriendEntivityK.getFeedPrivUser())){
                open_nolookme = true ;
                switch_nolookme.setChecked(true);
            }
            if ("0".equals(imFriendEntivityK.getFeedPrivFriend())){
                open_nolookher = true ;
                switch_nolookher.setChecked(true);
            }
        }

        switch_nolookme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if ((open_nolookme && isChecked) || (!open_nolookme && !isChecked)){

                }else {
                    setFeedAuth(isChecked?"0":"1","0");
                    open_nolookme = isChecked ;
                }

            }
        });
        switch_nolookher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if ((open_nolookher && isChecked) || (!open_nolookher && !isChecked)){

                }else {
                    setFeedAuth(isChecked?"0":"1","1");
                    open_nolookher = isChecked ;
                }
            }
        });

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_set_circle_complaint ;
    }

    private void setFeedAuth(final String priv, final String direct) {
        Log.i("info", "setFeedAuth: ================="+ToolsUtils.getMyUserId()+"==friendId=="+friendId+"==priv=="+priv+"==direct=="+direct);
        mPgService.setFeedAuth(ToolsUtils.getMyUserId(), friendId, priv, direct).subscribe(new AbsAPICallback<ValidateEntivity>() {
            @Override
            protected void onResultError(ApiException ex) {

            }

            @Override
            public void onNext(ValidateEntivity circlepreview) {
                if ("0".equals(direct)){//不看我
                    if ("0".equals(priv)){//FALSE
                        imFriendEntivityK.setFeedPrivUser("0");
                    }else {
                        imFriendEntivityK.setFeedPrivUser("1");
                    }
                }else {//不看他
                    if ("0".equals(priv)){//FALSE
                        imFriendEntivityK.setFeedPrivFriend("0");
                    }else {
                        imFriendEntivityK.setFeedPrivFriend("1");
                    }
                }
                imFriendEntivityK.save();
                showToast(getResources().getString(R.string.set_success));
            }
        });
    }

}
